#!/usr/bin/env php
<?php

/*

Desktopd Wallpaper
Copyright (C) 2015  Desktopd Developer(s)

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.

https://www.gnu.org/licenses/agpl.html

@kind entry-executable

*/


namespace Desktopd\Wallpaper;

use Desktopd\PlatformTools;


/*
    Autoloader
*/
require_once dirname(__DIR__) . '/autoloader.php';


/*
    Load settings
*/
require_once dirname(__DIR__) . '/DefaultSettings.php';


PlatformTools\Log::setLogFile(__DIR__ . '/log_wallpaper-agent.log');
PlatformTools\Log::setLogLevel(PlatformTools\Log::DEBUG);
PlatformTools\Log::setConsoleLogLevel(PlatformTools\Log::INFO);


/*
    Print what is this program
*/
PlatformTools\Log::log("Starting: $desktopdProductName, version $desktopdVersion", PlatformTools\Log::INFO);
PlatformTools\Log::log("Module: Wallpaper Agent", PlatformTools\Log::INFO);


PlatformTools\Log::log("Searching available pictures...", PlatformTools\Log::INFO);
$wallpapers = new WallpaperStore($wallpaperDataDirectory, $wallpaperCacheTTL);
$wallpapers->addLogger(new PlatformTools\CallbackLogger(function ($message, $level) {
    PlatformTools\Log::log($message, $level);
}));
$wallpapers->addSource($pictureClientPictureDirectory);
PlatformTools\Log::log(
    sprintf("%d picture(s) available", count($wallpapers))
    , PlatformTools\Log::INFO
);

while (true) {
    $wallpapers->change($wallpaperColorProb);
    
    $sleep = PlatformTools\Random::exponentialByMean(
        1e6 * max(0, $wallpaperChangeMeanInterval)
    );
    $s = round($sleep * 1e-6);
    PlatformTools\Log::log("Sleeping for about $s seconds...", PlatformTools\Log::NOTICE);
    usleep($sleep);
}


// vim: ts=4 et ai

